package net;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPechoServer {

    private static final int PORT = 4862;
    public static void main(String[] args) throws IOException {
        System.out.println("UDPServer has already started... waiting for the connection");
        System.out.println("-------------------------------------------------------");
        //创建DatagramSocket，绑定端口
        DatagramSocket socket = new DatagramSocket(PORT);
        //创建数据报，对接收端而言这个packet是用来接收数据的，所以不需要有内容
        byte[] bytes = new byte[1024];
        DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
        //通过socket来接收数据，接收到的数据传入了packet中
        socket.receive(packet);
        System.out.println("UDPServer has already received data... waiting for decoding");
        System.out.println("-------------------------------------------------------");
        //通过IOStream来获取packet中的信息
        System.out.printf("ClientIP:%s ClientPort%d\n",packet.getSocketAddress(), packet.getPort());
        System.out.printf("The Raw data should be:%s\n", packet.getData());
        System.out.printf("The text data should be:%s\n", packet.getData().toString());
    }

}
